package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.R;
import com.sofwin.annotation.Log;
import com.sofwin.pojo.SysRole;
import com.sofwin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2023/5/17 15:57
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("list")
    public String list(){
        // 跳转到list页面
        return "role/list";
    }
    @GetMapping("data")
    @ResponseBody
    @Log(module = "查询角色信息")
    public R data(SysRole role, @RequestParam(required = true,defaultValue = "1") Integer pageNumber,
                  @RequestParam(required = true,defaultValue = Constants.PAGE_SIZE) Integer pageSize){
        PageInfo<SysRole> page = roleService.quryByPage(role, pageNumber, pageSize);
        return R.success(page);
    }
    @GetMapping("toEdit")
    public String toEdit(Integer id, Model model){
        if(id!=null){
            SysRole role = roleService.queryById(id);
            model.addAttribute("role",role);
        }
        return "role/edit";
    }
    @PostMapping("save")
    @ResponseBody
    @Log(module = "保存角色信息")
    public R save(SysRole role){
        boolean status = roleService.saveOrUpdateRole(role);
        return R.success(status);
    }
    @DeleteMapping("delete/{id}")
    @ResponseBody
    @Log(module = "删除单个角色")
    public R delete(@PathVariable Integer id){
        boolean status = roleService.removeById(id);
        return R.success(status);
    }
    @RequestMapping("deletes")
    @ResponseBody
    @Log(module = "批量删除角色")
    public R deletes(@RequestParam(name="ids[]") Integer[] ids){
        boolean status = roleService.removeById(ids);
        return R.success(status);
    }
    @GetMapping("tree")
    public String tree(Integer roleId,Model model){
        model.addAttribute("roleId",roleId);
        return "role/tree";
    }
    @RequestMapping("savePermission")
    @ResponseBody
    public R savePermission(@RequestParam(name="ids[]") Integer[] ids,Integer roleId){
        boolean status = roleService.saveRoleResources(ids, roleId);
        return R.success(status);
    }
}
